package com.bcxj.servicemap.remote;


import com.bcxj.internalcommon.constant.AmapConfigConstants;
import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.response.TrackResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * ClassName: TrackClient
 * Package: com.bcxj.servicemap.remote
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/11/2 16:31
 * @Version 1.0
 * @Since 1.0
 */
@Component
public class TrackClient {

    @Value("${amap.key}")
    private String key;
    @Value("${amap.sid}")
    private String sid;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 添加轨迹
     * @param tid
     * @return
     */
    public ResponseResult<TrackResponse> add(String tid) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TRACK_ADD_URL);
        url.append("?key=").append(key);
        url.append("&sid=").append(sid);
        url.append("&tid=").append(tid);

        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        // 解析结果
        String body = stringResponseEntity.getBody();

        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");

        String trid = data.getString("trid");
        String trname = "";
        if(data.has("trname")) {
            trname = data.getString("trname");
        }
        TrackResponse trackResponse = new TrackResponse(trid, trname);
        return ResponseResult.success(trackResponse);
    }

}
